﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace File_Encription
{
    class FileEncripion
    {
        public static void EncryptFile(string filename, string outputFilename)
        {
            const int BUFFER_SIZE = 5;
            int bufferCurrentSize = BUFFER_SIZE;
            FileStream inputFile = File.Open(filename, FileMode.Open);
            using (inputFile)
            {
                byte[] buffer = new byte[BUFFER_SIZE];
               int bytesRead= inputFile.Read(buffer, 0, buffer.Length);
               FileStream outputFile = File.Open(outputFilename, FileMode.OpenOrCreate);
               using (outputFile)
               {
                   while (bytesRead != 0)
                   {
                       if (bytesRead < BUFFER_SIZE)
                       {
                           bufferCurrentSize = bytesRead;
                       }
                       RotateBytesRight(buffer, bufferCurrentSize);
                       outputFile.Write(buffer, 0, bufferCurrentSize);
                       bytesRead = inputFile.Read(buffer, 0, buffer.Length);
                   }
               }
            }
        }

        private static void RotateBytesRight(byte[] bytes, int bufferSize)
        {
            byte lastByte = bytes[bufferSize - 1];

            for (int i = bufferSize-1; i>0; i--)
            {
                bytes[i] = bytes[i-1]; 
            }

            bytes[0] = lastByte;
        }

        public static void DecryptFile(string filename, string outputFilename)
        {
            const int BUFFER_SIZE = 5;
            int bufferCurrentSize = BUFFER_SIZE;
            FileStream inputFile = File.Open(filename, FileMode.Open);
            using (inputFile)
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead = inputFile.Read(buffer, 0, buffer.Length);
                FileStream outputFile = File.Open(outputFilename, FileMode.OpenOrCreate);
                using (outputFile)
                {
                    while (bytesRead != 0)
                    {
                        if (bytesRead < BUFFER_SIZE)
                        {
                            bufferCurrentSize = bytesRead;
                        }
                        RotateBytesLeft(buffer, bufferCurrentSize);
                        outputFile.Write(buffer, 0, bufferCurrentSize);
                        bytesRead = inputFile.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }

        private static void RotateBytesLeft(byte[] bytes, int bufferSize)
        {
            byte lastByte = bytes[0];

            for (int i = 0; i < bufferSize-1; i++)
            {
                bytes[i] = bytes[i + 1];
            }

            bytes[bufferSize-1] = lastByte;
        }

        static void Main()
        {
        }
    }
}
